package com.prototype.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.prototype.constants.AdminStatus;
import com.prototype.dao.AdminDAO;
import com.prototype.po.Admin;
import com.prototype.service.AdminSerice;
import com.prototype.util.DateUtils;
import com.prototype.util.StringUtils;
import com.prototype.web.vo.AdminVO;

/**
 * 管理员业务类service实现
 * 
 * @author eg366
 * 
 */
@Service("adminService")
public class AdminServiceImpl implements AdminSerice {

	@Autowired
	private AdminDAO adminDAO;

	/**
	 * 根据登录名和密码查询Admin对象
	 */
	@Override
	public Admin getByNamePwd(String loginName, String password) {
		password = DigestUtils.md5Hex(password);
		List<Admin> list = this.adminDAO.findByNamePwd(loginName, password);
		return (list == null || list.size() != 1) ? null : list.get(0);
	}

	/**
	 * 创建管理员
	 */
	@Override
	public void addAdmin(AdminVO vo) {
		Admin admin = new Admin();
		admin.setUserName(vo.getUserName());
		admin.setLoginName(vo.getLoginName());
		admin.setPassword(DigestUtils.md5Hex(vo.getPassword()));
		admin.setCreateDate(new Date());
		admin.setStatus(AdminStatus.NORMAL.code);
		adminDAO.save(admin);
	}

	/**
	 * 列表查询总条数
	 */
	@Override
	public int getCountByParams(Map<String, Object> queryParams) {
		return this.adminDAO
				.findCountByCriteria(buildListQueryCriteria(queryParams));
	}

	/**
	 * 列表查询数据
	 */
	@Override
	public List<AdminVO> listGrid(Map<String, Object> queryParams,
			int startNum, int recordNum) {
		DetachedCriteria criteria = buildListQueryCriteria(queryParams);
		criteria.addOrder(Order.desc("createDate"));
		List<Admin> list = this.adminDAO.findByCriteria(criteria, startNum,
				recordNum);
		List<AdminVO> voList = new ArrayList<>();
		for (Admin admin : list) {
			AdminVO vo = new AdminVO(admin.getUserName(), admin.getLoginName());
			vo.setId(admin.getId());
			vo.setCreateDate(DateUtils.formatFull14StyleByDate(admin
					.getCreateDate()));
			vo.setLastLoginDate(DateUtils.formatFull14StyleByDate(admin
					.getLastLoginDate()));
			vo.setStatus(admin.getStatus());
			vo.setStatusStr(AdminStatus.get(admin.getStatus()));
			voList.add(vo);
		}
		return voList;
	}

	/**
	 * 构建列表查询条件DetachedCriteria
	 */
	private DetachedCriteria buildListQueryCriteria(
			Map<String, Object> queryParams) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Admin.class);
		String userName = StringUtils.trimToEmpty4Obj(queryParams
				.get("userName"));
		if (StringUtils.isNotEmpty(userName))
			criteria.add(Restrictions.like("userName", userName,
					MatchMode.ANYWHERE));
		String loginName = StringUtils.trimToEmpty4Obj(queryParams
				.get("loginName"));
		if (StringUtils.isNotEmpty(loginName))
			criteria.add(Restrictions.like("loginName", loginName,
					MatchMode.ANYWHERE));
		return criteria;
	}

	/**
	 * 根据登录名查询
	 */
	@Override
	public List<Admin> getByLoginName(String loginName) {
		Map<String, Object> queryParams = new HashMap<String, Object>();
		queryParams.put("loginName", loginName);
		return this.adminDAO
				.findByCriteria(buildListQueryCriteria(queryParams));
	}

	/**
	 * 修改操作员
	 */
	@Override
	public void editByAdminVO(AdminVO vo) {
		Admin admin = (Admin) this.adminDAO.getObject(vo.getId());
		admin.setUserName(vo.getUserName());
		admin.setLoginName(vo.getLoginName());
		this.adminDAO.update(admin);
	}

	/**
	 * 根据ID禁用操作员
	 */
	@Override
	public void disableById(Integer id) {
		Admin admin = (Admin) this.adminDAO.getObject(id);
		admin.setStatus(AdminStatus.DISABLE.code);
		this.adminDAO.update(admin);
	}

	/**
	 * 更新
	 */
	@Override
	public void update(Admin admin) {
		this.adminDAO.update(admin);
	}

	/**
	 * 根据ID查询
	 */
	@Override
	public Admin getById(Integer id) {
		return (Admin) this.adminDAO.getObject(id);
	}

	/**
	 * 重设密码
	 */
	@Override
	public void resetPwd(Integer id, String newPwd) {
		Admin admin  = this.getById(id);
		admin.setPassword(DigestUtils.md5Hex(newPwd));
		this.update(admin);
	}

	/**
	 * 根据ID删除
	 */
	@Override
	public void deleteById(Integer id) {
		this.adminDAO.deletebyId(id);
	}
}
